package com.dm.wallpaper.library.fraction;

import com.dm.wallpaper.library.ResourceTable;
import com.dm.wallpaper.library.abilityPackage.WallpaperBoardApplication;
import com.dm.wallpaper.library.ablility.WallpaperAbility;
import com.dm.wallpaper.library.adapters.LanguageListProvider;
import com.dm.wallpaper.library.helpers.LocaleHelper;
import com.dm.wallpaper.library.helpers.WallpaperHelper;
import com.dm.wallpaper.library.preferences.Preferences;
import com.dm.wallpaper.library.utils.Log;
import com.dm.wallpaper.library.utils.Toast;
import com.dm.wallpaper.library.utils.SystemUtils;
import com.dm.wallpaper.library.view.MiCommonDialog;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.components.element.VectorElement;
import ohos.agp.utils.LayoutAlignment;

import java.io.File;
import java.text.DecimalFormat;
import java.text.NumberFormat;

public class SettingsFraction extends Fraction implements Component.ClickedListener {
    Component component;
    DirectionalLayout settingContent;

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        component = scatter.parse(ResourceTable.Layout_fraction_setting_favorite, container, false);
        return component;
    }

    @Override
    protected void onActive() {
        super.onActive();
        component.findComponentById(ResourceTable.Id_left_mune_icon).setClickedListener(this);
        ((Text) component.findComponentById(ResourceTable.Id_navigation_view_settings)).setText(ResourceTable.String_navigation_view_settings);
        settingContent = (DirectionalLayout) component.findComponentById(ResourceTable.Id_setting_content);
        settingContent.removeAllComponents();

        try {
            addDataAndCache();
            addTheme();
            addWallpaper();
            addlanguage();
            addOther();
        } catch (Exception e) {
            Log.i("SettingFraction.onActive([]):40  错误:" + e.getMessage());
        }
    }

    private void addDataAndCache() throws Exception {

        NumberFormat formatter = new DecimalFormat("#0.00");

        Component component = LayoutScatter.getInstance(getFractionAbility()).parse(ResourceTable.Layout_settingitem_data_cache, null, false);
        Text headName = (Text) component.findComponentById(ResourceTable.Id_item_head_name);
        Image headImage = (Image) component.findComponentById(ResourceTable.Id_item_head_image);
        headName.setText(ResourceTable.String_pref_data_header);
        VectorElement vectorElement = new VectorElement(getFractionAbility(), ResourceTable.Graphic_svg_toolbar_storage);
        headImage.setImageElement(vectorElement);
        String cache = getFractionAbility().getResourceManager().getElement(ResourceTable.String_pref_data_cache_size).getString();


        long cacheSize = SystemUtils.getDirectorySize(getFractionAbility().getCacheDir());
//        int cacheSize = getAbilityManager().getAppMemory();

        ((Text) component.findComponentById(ResourceTable.Id_cache_size)).setText(cache +
                formatter.format(cacheSize) + " MB");
        settingContent.addComponent(component);


        component.findComponentById(ResourceTable.Id_cache_layout).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                MiCommonDialog commonDialog = new MiCommonDialog(component.getContext());
                Component.ClickedListener clickedListener = new Component.ClickedListener() {
                    @Override
                    public void onClick(Component component) {
                        if (component.getId() == ResourceTable.Id_cancal) {
                            commonDialog.destroy();
                        } else if (component.getId() == ResourceTable.Id_confirm) {

                            File cache = WallpaperBoardApplication.getInstance().getCacheDir();
                            try {
                                SystemUtils.clearDirectory(cache);
                            } catch (Exception e) {

                            }

                            try {
                                String cacheString = getFractionAbility().getResourceManager().getElement(ResourceTable.String_pref_data_cache_size).getString();
                                ((Text) component.findComponentById(ResourceTable.Id_cache_size)).setText(cacheString +
                                        formatter.format(SystemUtils.getDirectorySize(getFractionAbility().getCacheDir())) + " MB");
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                            commonDialog.destroy();
                        }
                    }
                };
                Component content = LayoutScatter.getInstance(component.getContext()).parse(ResourceTable.Layout_dialog_show_comfirm_and_cancellayout, null, false);
                content.findComponentById(ResourceTable.Id_cancal).setClickedListener(clickedListener);
                content.findComponentById(ResourceTable.Id_confirm).setClickedListener(clickedListener);
                commonDialog.setAutoClosable(true);
                commonDialog.setContentCustomComponent(content).setAlignment(LayoutAlignment.CENTER).setTransparent(true);
                commonDialog.show();
            }
        });
    }

    private void addTheme() {
        Component component = LayoutScatter.getInstance(getFractionAbility()).parse(ResourceTable.Layout_settingitem_theme, null, false);
        Text headName = (Text) component.findComponentById(ResourceTable.Id_item_head_name);
        Image headImage = (Image) component.findComponentById(ResourceTable.Id_item_head_image);
        headName.setText(ResourceTable.String_pref_theme_header);
        VectorElement vectorElement = new VectorElement(getFractionAbility(), ResourceTable.Graphic_svg_toolbar_theme);
        headImage.setImageElement(vectorElement);
        settingContent.addComponent(component);
    }

    private void addWallpaper() {
        Component component = LayoutScatter.getInstance(getFractionAbility()).parse(ResourceTable.Layout_settingitem_wallpaper, null, false);
        Text headName = (Text) component.findComponentById(ResourceTable.Id_item_head_name);
        Image headImage = (Image) component.findComponentById(ResourceTable.Id_item_head_image);
        Text saveLoaction = (Text) component.findComponentById(ResourceTable.Id_save_loaction);
        Text qualit_type = (Text) component.findComponentById(ResourceTable.Id_qualit_type);
        DirectionalLayout qualit_type_layout = (DirectionalLayout) component.findComponentById(ResourceTable.Id_qualit_type_layout);
        qualit_type_layout.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                showQualitDialog(qualit_type);
            }
        });


        headName.setText(ResourceTable.String_pref_wallpaper_header);
        VectorElement vectorElement = new VectorElement(getFractionAbility(), ResourceTable.Graphic_svg_toolbar_wallpapers);
        headImage.setImageElement(vectorElement);


        qualit_type.setText(Preferences.get().isHighQualityPreviewEnabled() ? ResourceTable.String_pref_wallpaper_high_quality_preview_high : ResourceTable.String_pref_wallpaper_high_quality_preview_low);

        saveLoaction.setText(WallpaperHelper.getDefaultWallpapersDirectory(getFractionAbility()).toString());
        settingContent.addComponent(component);
    }

    private void showQualitDialog(Text qualit_type) {
        MiCommonDialog categriesdialog = new MiCommonDialog(getFractionAbility());
        Component content = LayoutScatter.getInstance(getFractionAbility()).parse(ResourceTable.Layout_qualit_layout, null, false);

        RadioButton high = (RadioButton) content.findComponentById(ResourceTable.Id_high);
        RadioButton low = (RadioButton) content.findComponentById(ResourceTable.Id_low);
        if (Preferences.get().isHighQualityPreviewEnabled()) {
            high.setChecked(true);
        } else {
            low.setChecked(true);
        }

        categriesdialog.setAutoClosable(true);
        categriesdialog.setContentCustomComponent(content).setAlignment(LayoutAlignment.CENTER).setTransparent(true);
        categriesdialog.addDialogListener(new MiCommonDialog.CommonDialogListener() {
            @Override
            public void hide() {
            }

            @Override
            public void destroy() {
                Preferences.get().setHighQualityPreviewEnabled(high.isChecked());
                qualit_type.setText(Preferences.get().isHighQualityPreviewEnabled() ? ResourceTable.String_pref_wallpaper_high_quality_preview_high : ResourceTable.String_pref_wallpaper_high_quality_preview_low);
            }
        });
        categriesdialog.show();
    }


    private void addlanguage() {
        Component component = LayoutScatter.getInstance(getFractionAbility()).parse(ResourceTable.Layout_settingitem_language, null, false);
        Text headName = (Text) component.findComponentById(ResourceTable.Id_item_head_name);
        Image headImage = (Image) component.findComponentById(ResourceTable.Id_item_head_image);
        headName.setText(ResourceTable.String_pref_language_header);
        VectorElement vectorElement = new VectorElement(getFractionAbility(), ResourceTable.Graphic_svg_toolbar_language);
        headImage.setImageElement(vectorElement);
        Text language = (Text) component.findComponentById(ResourceTable.Id_now_language);
        language.setText(LocaleHelper.getCurrentLanguage(getFractionAbility()).getName());
        language.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                MiCommonDialog commonDialog = new MiCommonDialog(component.getContext());
                commonDialog.addDialogListener(new MiCommonDialog.CommonDialogListener() {
                    @Override
                    public void hide() {

                    }

                    @Override
                    public void destroy() {
                        if (language != null) {
                            language.setText(LocaleHelper.getCurrentLanguage(getFractionAbility()).getName());
                            SettingsFraction.this.onActive();
                        }
                    }
                });

                Component content = LayoutScatter.getInstance(component.getContext()).parse(ResourceTable.Layout_language_list_layout, null, false);
                ListContainer listContainer = (ListContainer) content.findComponentById(ResourceTable.Id_language_list);
                listContainer.setItemProvider(new LanguageListProvider(getFractionAbility(), commonDialog));
                commonDialog.setAutoClosable(true);
                commonDialog.setContentCustomComponent(content).setAlignment(LayoutAlignment.CENTER).setTransparent(true);
                commonDialog.show();
            }
        });
        settingContent.addComponent(component);
    }

    private void addOther() {
        Component component = LayoutScatter.getInstance(getFractionAbility()).parse(ResourceTable.Layout_settingitem_language, null, false);
        Text headName = (Text) component.findComponentById(ResourceTable.Id_item_head_name);
        Image headImage = (Image) component.findComponentById(ResourceTable.Id_item_head_image);
        headName.setText(ResourceTable.String_pref_others_header);
        VectorElement vectorElement = new VectorElement(getFractionAbility(), ResourceTable.Graphic_svg_toolbar_others);
        headImage.setImageElement(vectorElement);
        Text now_language = (Text) component.findComponentById(ResourceTable.Id_now_language);
        now_language.setText(ResourceTable.String_pref_others_reset_tutorial);
        now_language.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Toast.show(component.getContext(), ((Text) component).getText(), LayoutAlignment.BOTTOM);
            }
        });
        settingContent.addComponent(component);
    }


    @Override
    public void onClick(Component component) {
        if (component.getId() == ResourceTable.Id_left_mune_icon) {
            if (getFractionAbility() instanceof WallpaperAbility) {
                ((WallpaperAbility) getFractionAbility()).showMunePop();
            }
        }
    }
}
